﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Common;
using SYH.Config;

namespace SYH.System.DAL
{
    public class SystemAdmin
    {
        public SystemAdmin()
        { }
        #region  基本方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SystemAdmin");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            object obj = SqlHelper.ExecuteScalar(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return Common.TypeParse.ObjectToInt(obj, 0) > 0;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SYH.System.Model.SystemAdmin model)
        {
            //StringBuilder strSql = new StringBuilder();
            //strSql.Append("insert into SystemAdmin(");
            //strSql.Append("UserName,PassWord,RoleId,Status)");
            //strSql.Append(" values (");
            //strSql.Append("@UserName,@PassWord,@RoleId,@Status)");
            //strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@PassWord", SqlDbType.NVarChar,50),
					new SqlParameter("@RoleId", SqlDbType.Int,4),
					new SqlParameter("@Status", SqlDbType.Int,4)};
            parameters[0].Value = model.UserName;
            parameters[1].Value =model.PassWord;
            parameters[2].Value = model.RoleId;
            parameters[3].Value = model.Status;

            int obj = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.StoredProcedure, "SystemAdmin_Add", parameters);
            return obj;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SYH.System.Model.SystemAdmin model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SystemAdmin set ");
            strSql.Append("UserName=@UserName,");
            strSql.Append("PassWord=@PassWord,");
            strSql.Append("RoleId=@RoleId,");
            strSql.Append("Status=@Status");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@PassWord", SqlDbType.NVarChar,50),
					new SqlParameter("@RoleId", SqlDbType.Int,4),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.UserName;
            parameters[1].Value = model.PassWord;
            parameters[2].Value = model.RoleId;
            parameters[3].Value = model.Status;
            parameters[4].Value = model.Id;

            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return rows > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update  SystemAdmin set Status=-1 ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return rows > 0;
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SystemAdmin ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
            return rows > 0;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH.System.Model.SystemAdmin GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,UserName,PassWord,RoleId,Status from SystemAdmin ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            SYH.System.Model.SystemAdmin model = new SYH.System.Model.SystemAdmin();
            DataSet ds = SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH.System.Model.SystemAdmin DataRowToModel(DataRow row)
        {
            SYH.System.Model.SystemAdmin model = new SYH.System.Model.SystemAdmin();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["UserName"] != null)
                {
                    model.UserName = row["UserName"].ToString();
                }
                if (row["PassWord"] != null)
                {
                    model.PassWord = row["PassWord"].ToString();
                }
                if (row["RoleId"] != null && row["RoleId"].ToString() != "")
                {
                    model.RoleId = int.Parse(row["RoleId"].ToString());
                }
                if (row["Status"] != null && row["Status"].ToString() != "")
                {
                    model.Status = int.Parse(row["Status"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,UserName,PassWord,RoleId,Status ");
            strSql.Append(" FROM SystemAdmin ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,UserName,PassWord,RoleId,Status ");
            strSql.Append(" FROM SystemAdmin ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM SystemAdmin ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
            return Common.TypeParse.ObjectToInt(obj, 0);
        }

        #endregion

        #region 管理员列表
        public DataSet GetList(string strWhere, bool b)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select sa.Id,sa.UserName,sa.PassWord,sa.Status,sr.RoleName ");
            strSql.Append(" from SystemAdmin sa inner join SystemRole sr on sa.RoleId=sr.Id ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString());
        }

        #endregion

        #region 验证登陆
        public SYH.System.Model.SystemAdmin GetModel(string userName, string passWord)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,UserName,PassWord,RoleId,Status from SystemAdmin ");
            strSql.Append(" where UserName=@UserName and PassWord=@PassWord and Status>=0");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
                    new SqlParameter("@PassWord", SqlDbType.NVarChar,50)
			};
            parameters[0].Value = userName;
            parameters[1].Value = passWord;

            SYH.System.Model.SystemAdmin model = new SYH.System.Model.SystemAdmin();
            DataSet ds = SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region 修改密码
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="OldPwd"></param>
        /// <param name="NewPwd"></param>
        /// <returns>0:旧密码不正确 1:成功 -1:失败</returns>
        public int UpdatePassword(int UserId, string OldPwd, string NewPwd)
        {
            SqlParameter returnParam = new SqlParameter("@returnValue", SqlDbType.Int);
            returnParam.Direction = ParameterDirection.ReturnValue;
            SqlHelper.ExecuteNonQuery(
                WebConfig.SqlConnString,
                CommandType.StoredProcedure,
                "SystemAdmin_UpdatePassword",
                new SqlParameter("@UserId", UserId),
                new SqlParameter("@OldPwd", OldPwd),
                new SqlParameter("@NewPwd", NewPwd),
                returnParam);
            return TypeParse.ObjectToInt(returnParam.Value, -1);
        }
        #endregion


    }
}
